package cn.new7as.cas5s.tool.phut.funs

import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.provider.CalendarContract
import android.text.TextUtils
import org.json.JSONArray
import org.json.JSONObject
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date


@SuppressLint("Range")
class CaleHelper {
    companion object {
        private var mSingleton: CaleHelper? = null
            get() {
                if (field == null) {
                    field = CaleHelper()
                }
                return field
            }

        @Synchronized
        fun getInstance(): CaleHelper {
            return mSingleton!!
        }
    }
    
    
    
    @SuppressLint("Recycle")
     fun getCalenderSchedule(context: Context): String {
        val contentResolver = context.contentResolver
        val eventCursor = contentResolver.query(
            Uri.parse("content://com.android.calendar/events"), null,
            null, null, "dtstart" + " DESC"
        )
        val jsonArray = JSONArray()
        while (eventCursor!!.moveToNext()) {
            var npyWjmbUszcCwa = ""
            var startTime = ""
            var endTime = ""
            var eventTitle = ""
            var description = ""
            var location = ""
            var week = ""
            val calID =
                eventCursor.getLong(eventCursor.getColumnIndex(CalendarContract.Calendars._ID))
            if (calID != null) {
                npyWjmbUszcCwa = calID.toString()
            }
            val strTitle = eventCursor.getString(eventCursor.getColumnIndex("title"))
            if (!TextUtils.isEmpty(strTitle)) {
                eventTitle = strTitle
            }
            val strDes = eventCursor.getString(eventCursor.getColumnIndex("description"))
            if (!TextUtils.isEmpty(strDes)) {
                description = strDes
            }
            val strLoca = eventCursor.getString(eventCursor.getColumnIndex("eventLocation"))
            if (!TextUtils.isEmpty(strLoca)) {
                location = strLoca
            }

            val strTime = timeStamp2Date(
                eventCursor.getString(eventCursor.getColumnIndex("dtstart")).toLong()
            )
            if (!TextUtils.isEmpty(strTime)) {
                startTime = strTime!!
                week = "" + getWeek(startTime)
            }

            val dtend = eventCursor.getString(eventCursor.getColumnIndex("dtend"))
            if (dtend != null) {
                val strEndTime = timeStamp2Date(dtend.toLong())
                if (!TextUtils.isEmpty(strEndTime)) {
                    endTime = strEndTime!!
                }
            }

            val reminders = getReminders(contentResolver, npyWjmbUszcCwa)

            val jsonObject = JSONObject()
            jsonObject.put("npyWjmbUszcCwa", npyWjmbUszcCwa)
            jsonObject.put("izrwGxscfLzwa", eventTitle)
            jsonObject.put("ufoEfkdTuaTsbvp", startTime)
            jsonObject.put("gfhWsxkCyjCckmc", endTime)
            jsonObject.put("jjfnFbcuWlyDzay", location)
            jsonObject.put("yetsBnsAenIsmvo", description)
            jsonObject.put("romqUesGmxczLbgAxxgf", week)
            jsonObject.put("nwioCvzgrQhrc", reminders)
            jsonArray.put(jsonObject)
        }
        return jsonArray.toString()
    }


    private fun getReminders(contentResolver: ContentResolver, id: String): JSONArray {
        val remindersCursor = contentResolver.query(
            CalendarContract.Reminders.CONTENT_URI,
            null,
            CalendarContract.Reminders.EVENT_ID + "=?",
            arrayOf(id),
            null,
            null
        )
        val jsonArray = JSONArray()
        while (remindersCursor!!.moveToNext()) {
            val reminderID =
                remindersCursor.getInt(remindersCursor.getColumnIndex(CalendarContract.Reminders.EVENT_ID))
            val minutes =
                remindersCursor.getInt(remindersCursor.getColumnIndex(CalendarContract.Reminders.MINUTES))
            val method =
                remindersCursor.getInt(remindersCursor.getColumnIndex(CalendarContract.Reminders.METHOD))

            val jsonObject = JSONObject()
            jsonObject.put("reminderID", reminderID)
            jsonObject.put("minutes", minutes)
            jsonObject.put("remindersMethod", method)
            jsonArray.put(jsonObject)
        }
        return jsonArray
    }

    private fun timeStamp2Date(time: Long): String? {
        val format = "yyyy-MM-dd HH:mm:ss"
        val sdf = SimpleDateFormat(format)
        return sdf.format(Date(time))
    }

    private fun getWeek(pTime: String?): Int {
        var Week = 0
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val c = Calendar.getInstance()
        try {
            c.time = format.parse(pTime)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        Week=c[Calendar.DAY_OF_WEEK]-1

        return Week
    }


}